package com.blue.http.config;

import com.blue.http.filter.Filter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 过滤器配置
 *
 * @author zhengjin
 * @since 1.0 2018年03月21日
 */
public class InvokerFilterConfig implements InitializingBean, Comparable<InvokerFilterConfig>
{
	private Class<?> clazz;
	private Filter object;
	private int order = 10;
	private List<String> filterList = new ArrayList<>();
	private List<String> excludeList = new ArrayList<>();

	public InvokerFilterConfig()
	{
	}

	@Override
	public void afterPropertiesSet() throws Exception
	{
		if (object == null)
			throw new IllegalArgumentException("InvokerFilter 实例不能为空");

		if (filterList == null || filterList.isEmpty())
			throw new IllegalArgumentException("过滤URL不能为空");

	}

	@Override
	public int compareTo(InvokerFilterConfig o)
	{
		return order - o.order;
	}

	public void setObject(Object object)
	{
		Assert.notNull(object, "实例不能为空");
		if (!(object instanceof Filter))
			throw new IllegalArgumentException(object.getClass().getName() + " 没有实现 Filter 接口");

		this.object = (Filter) object;
		this.clazz = this.object.getClass();
	}

	public void setFilters(String filters)
	{
		Assert.hasText(filters, "URL地址不能为空");
		for (String filter : filters.split(","))
		{
			filterList.add(filter.trim());
		}
	}

	public void setExcludes(String excludes)
	{
		if (excludes == null || excludes.isEmpty())
			return;

		for (String exclude : excludes.split(","))
		{
			excludeList.add(exclude.trim());
		}
	}

	public void setFilterList(List<String> filterList)
	{
		this.filterList = filterList;
	}

	public void setExcludeList(List<String> excludeList)
	{
		this.excludeList = excludeList;
	}

	public int getOrder()
	{
		return order;
	}

	public void setOrder(int order)
	{
		this.order = order;
	}

	public Filter getObject()
	{
		return object;
	}

	public List<String> getFilterList()
	{
		return filterList;
	}

	public List<String> getExcludeList()
	{
		return excludeList;
	}

	public Class<?> getClazz()
	{
		return clazz;
	}
}
